<!DOCTYPE html>
<html>
    <head>
        <title>setMediaKeys() multiple times with different MediaKeys.</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <video></video>
        <script>
            promise_test(function(test)
            {
                var video = document.querySelector('video');
                var content = '../content/test-encrypted.webm';
                var keySystemAccess;
                var mediaKeys1;
                var mediaKeys2;

                assert_equals(video.mediaKeys, null);

                return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getConfigurationForFile(content)).then(function(access) {
                    keySystemAccess = access;
                    // Create a mediaKeys.
                    return keySystemAccess.createMediaKeys();
                }).then(function(result) {
                    mediaKeys1 = result;
                    assert_not_equals(mediaKeys1, null);
                    // Create a second mediaKeys.
                    return keySystemAccess.createMediaKeys();
                }).then(function(result) {
                    mediaKeys2 = result;
                    assert_not_equals(mediaKeys2, null);
                    // Set mediaKeys1 on video.
                    return video.setMediaKeys(mediaKeys1);
                }).then(function() {
                    assert_true(video.mediaKeys === mediaKeys1);
                    // Set mediaKeys2 on video (switching MediaKeys).
                    return video.setMediaKeys(mediaKeys2);
                }).then(function() {
                    assert_true(video.mediaKeys === mediaKeys2);
                    // Clear mediaKeys from video.
                    return video.setMediaKeys(null);
                }).then(function() {
                    assert_equals(video.mediaKeys, null);
                    // Set mediaKeys1 on video again.
                    return video.setMediaKeys(mediaKeys1);
                }).then(function() {
                    assert_true(video.mediaKeys === mediaKeys1);
                    // Load the media element to create the WebMediaPlayer.
                    video.src = content;
                    // Set mediaKeys2 on video (switching MediaKeys) not
                    // supported after WebMediaPlayer is created.
                    return video.setMediaKeys(mediaKeys2);
                }).then(function() {
                    assert_unreached('Switching mediaKeys after setting src should have failed.');
                }, function(error) {
                    assert_true(video.mediaKeys === mediaKeys1);
                    assert_equals(error.name, 'InvalidStateError');
                    assert_not_equals(error.message, '');
                    // Return something so the promise resolves properly.
                    return Promise.resolve();
                }).then(function() {
                    // Set null mediaKeys on video (clearing MediaKeys) not
                    // supported after WebMediaPlayer is created.
                    return video.setMediaKeys(null);
                }).then(function() {
                    assert_unreached('Clearing mediaKeys after setting src should have failed.');
                }, function(error) {
                    assert_true(video.mediaKeys === mediaKeys1);
                    assert_equals(error.name, 'InvalidStateError');
                    assert_not_equals(error.message, '');
                    return Promise.resolve();
                });
            }, 'setMediaKeys() multiple times with different MediaKeys.');
        </script>
    </body>
</html>
